# -*- coding: utf-8 -*-
# Created by liangfuting on 2020/7/9
# Copyright (c) 2020 liangfuting. All rights reserved.
from django.shortcuts import render,HttpResponse
from django.urls import path
from django.utils.safestring import mark_safe
from django.shortcuts import reverse,redirect
from stark.service.v1 import site,StarkHandler,get_choice_text,SearchOption
from app01.models import UserInfo,Depart


# class MyOption(SearchOption):

#     def get_db_condition(self,request,*args,**kwargs):
#         return {'id__gt':request.GET.get("nid")}

class DepartHandler(StarkHandler):
    # def get_urls(self):
    #     patterns = []
    #     # print(self.get_add_url_name, self.get_list_url_name, self.get_edit_url_name, self.get_del_url_name)
    #     patterns.append(path('', self.list_view, name="{}_list".format(self.model_name)))
    #     patterns.append(path('add/', self.add_view, name="{}_add".format(self.model_name)))
    #     return patterns
    list_display = ['id', 'title']
    order_list = ['id']
    search_list = ['title']
    # per_page_count = 3
    pass

class UserInfoHandler(StarkHandler):
    # def get_urls(self):
    #     patterns=super(UserInfoHandler,self).get_urls()
    #     patterns.append(path('detail/<int:pk>/',self.detail_view,name="{}_detail".format(self.model_name)))
    #     print(patterns)
    #     return patterns

    # def extra_urls(self):
    #     #两种方式都可以扩展，还是按老师视频的操作来吧。
    #     return [path('detail/<int:pk>/',self.detail_view,name="{}_detail".format(self.model_name))]
    #
    # def detail_view(self,request,pk):
    #     return HttpResponse('userinfo detail view {}'.format(pk))
    #定制显示的列



    # def display_edit(self,obj=None,is_header=None):
    #     if is_header:
    #         return '编辑'
    #     # esle:
    #     #     return "<a href='{}'>编辑</a>".format(self.get_edit_url_name,obj.pk)
    #     return mark_safe("<a href='{}/'>编辑</a>".format(obj.pk))
    #
    # def display_del(self,obj=None,is_header=None):
    #     if is_header:
    #         return '删除'
    #     # esle:
    #     #     return "<a href='{}'>编辑</a>".format(self.get_edit_url_name,obj.pk)
    #     return mark_safe("<a href='{}/'>删除</a>".format(obj.pk))

    #display_edit只是把名放在这里了，是类对他进行的调用，参数里面需要包含self
    #通过类进行调用称为是函数，通过对象调用是一个方法
    # list_display = ['name', 'age', 'email', 'depart',display_edit,display_del]


    #对于choices的选择
    # def display_gender(self,obj=None,is_header=None):
    #     if is_header:
    #         return '性别'
    #     return obj.get_gender_display()

    # list_display = ['name', 'age','gender', 'email', 'depart']
    list_display = ['name', 'age', get_choice_text('性别','gender'), 'email', 'depart']
    per_page_count= 10
    # search_list= ['name','email'] #精确查询
    search_list = ['name__contains','email__contains']#搜索的列，姓名或邮箱中有关键字，如果为空，则不显示模糊搜索栏
    # search_group = ['gender', 'depart']
    # search_group = [
    #     {'field': 'gender', 'db_condition': {}},
    #     {'field': 'depart', 'db_condition': {'id__gt': 5}},#如果是外键有关联有表的话
    # ]
    search_group = [
        # SearchOption('gender',text_func=lambda field_object:field_object[1]+'666'),
        SearchOption('gender',is_multi=True),
        SearchOption('depart', db_condition={'id__gt': 5}),  # 如果是外键有关联有表的话
    ]



    # def get_add_btn(self):
    #     #如果用户有添加权限，则显示添加按钮
    #     if self.has_add_btn:
    #         return "<a>添加</a>"
    #     return None




# print("app01 stark py")
# site.register(UserInfo,handler_class=UserInfoHandler,prev='private')
# site.register(UserInfo,prev='public')
site.register(UserInfo,UserInfoHandler)
site.register(Depart,DepartHandler)
# print(site.__dict__['_registry'])

